<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends  Controller {
    public function index()
    {

  //       $usersModel=M('users');
		// $users=$usersModel->order('id desc')->select();
  //       //dump($users);//输出数组

 
        $this->display();
    }
   



 public function checkLogin()
 {
        $code=I('post.verify'); 
         //获取用户在表单中输入的验证码字符串
    if(!$this->check_verify($code))
    $this->error('验证码错误'); //调用check_verify函数，将用户输入的验证码加密处理后和Session中已存的验证码进行比较

        $admin = M('admins');
      $map = array();
    
      $map['admin_name']  =  I('post.username');
      $map['password']  =  md5(I('post.password'));
      $res = $admin->where($map) -> select();
        if($res==null){
            $this->error('账号或密码错误');
        }else{
        
        $_SESSION['flag']="logok";
        $_SESSION['username']=$map['username'];
        $_SESSION['password']=$res[0]["password"];
      
        
        redirect(U('Index/count'));
   
        }
        }

    function check_verify($code, $id = '')
{
    $verify = new \Think\Verify();
    return $verify->check($code, $id);
}


  public function count()
    {

    	if(session('?flag'))
    	{
       
        $users = M('users');
        $count1 = $users->count();
        $this->count1=$count1;
        $lost = M('sight');
        $count2 = $lost->count();
        $this->count2=$count2;
        $find = M('search');
        $count3 = $find->count();
        $this->count3=$count3;
       
        $this->display();
     }

else {
	echo "It's a false!";
}

    	    }




    	    public function userList1()
    {

        $usersModel=M('users');
    $users=$usersModel->
order('id desc')->select();

   for ($i=0; $i<$count ; $i++) 
   { 
           
            $users[$i]['flag']=$i+1;
        }
        $this->users = $users;

        //dump($users);//输出数组
        $this->display();
    }

    public function Findlist1()
     {

        $findModel=M('search');
    $find=$findModel->order('id asc')->select();
    $this->find=$find;
        //dump($users);//输出数组
        $this->display();
    }
    public function lost()
     {

        $lostModel=M('sight');
    $lost=$lostModel->order('id asc')->
select();
    $this->lost=$lost;
        //dump($users);//输出数组
        $this->display();
    }
public function Ewmlist()
     {

        $goodsModel=M('goods');
    $goods=$goodsModel->order('id asc')->
select();
    $this->goods=$goods;
        //dump($users);//输出数组
        $this->display();
    }


      public function deluser($id)//deluser()


      /*
  $id=$_GET['id'];
  echo $id;
   $usersmodel=M('users'); 
   */

    {
      $id=I('id');
      $usersModel = M('users');
      $res = $usersModel->where("id=$id")->find();
      $photoPath = realpath("./Public/Uploads/photo/".$res['photo']);
      unlink($photoPath);
      if($usersModel->delete($id))
      {
        $this->success('删除成功');
      }else
      {
        $this->error('删除失败');
      }
    }
        // $usersmodel=M('users'); 
        // if (false!==$usersmodel->where("id=$id")->delete()) 
        // {
        //     $this->success('删除成功！');
        // } else 
        // {
        //     $this->error('删除失败');
        // }
    
  
      public function dellost($id)

{
      $id=I('id');
      $lostModel = M('lost');
      $res = $lostModel->where("id=$id")->find();
      $photoPath = realpath("./Public/Uploads/lostphoto/".$res['picture']);
      unlink($photoPath);
      if($lostModel->delete($id))
      {
        $this->success('删除成功');
      }else
      {
        $this->error('删除失败');
      }
    }

   

   public function delEwm($id)

{
      $id=I('id');
      $goodsModel = M('goods');
      $res = $goodsModel->where("id=$id")->find();
      $photoPath = realpath("./Public/Uploads/ewmphoto/".$res['qrpath']);
      unlink($photoPath);
      if($goodsModel->delete($id))
      {
        $this->success('删除成功');
      }else
      {
        $this->error('删除失败');
      }
    }



    public function delfind($id)


  {
      $id=I('id');
      $findModel = M('search');
      $res = $findModel->where("id=$id")->find();
      $photoPath = realpath("./Public/Uploads/search/".$res['picture']);
      unlink($photoPath);
      if($findModel->delete($id))
      {
        $this->success('删除成功');
      }else
      {
        $this->error('删除失败');
      }
    }





    public function editpswd($id)//editpswd()
    {

      /*$id=$_GET['id'];
    
      $data['password']= '123456';
      $usersmodel=M('users'); 
$res=$usersModel->where("id=$id")->save($data);
echo $usersModel->getLastSql();
  if(false!==$usersmodel->where("id=$id")->save($data))

      */
  $usersmodel=M('users'); 
  $usersmodel->password="123456";
  if(false!==$usersmodel->where("id=$id")->save()){
    $this->success('密码重置成功！');
  }else{
    $this->error('密码重置失败');
  }
  }

  public function state($id){
        $usersmodel=M('users');
        $state=$_GET['state'];
        if($_GET['state']=='0')
            $data['state'] = '1';
        else
            $data['state'] = '0';

        if(false!==$usersmodel->where("id=$id")->save($data))
        {
             $this->success('状态修改成功！');
         }
        else
        {
            $this->error('状态修改失败');
        }
    }

   public function detail1()
    {
 $id=$_GET['id'];
        $findModel=M('search');
    $find=$findModel->order('id asc')->where("id=$id")->
select();
    $this->find=$find;
        //dump($users);//输出数组
        $this->display();
    }
// public function data()
// {

// $lost=M('lost');//实例化news对象
// $find=M('find');//实例化news对象
// $goods=M('goods');//实例化news对象
// $users=M('users');//实例化news对象
// $this->goodsnum=$goods->count();
// $this->usersnum=$users->count();

// $this->lostno=$lost->where("isfinish=0") -> order('isfirst desc, id desc') ->limit(4) ->count();
// $this->findno=$find->where("isfinish=0") -> order('id desc') ->limit(4) ->count();
// $this->display();
// }
 
public function checkAdmin()
{

if(!session('?flag'))
{
  $this->error('无权访问此页面',U('Admin/index/index'));

}

}

public function _initialize()
{
       header("Content-type:text/html;charset=utf-8");
       
          if(ACTION_NAME!='index' && ACTION_NAME!='checkLogin' && ACTION_NAME!='verify')
          {
            if(null==session('flag')|| session('flag')!='logok')
            {
                redirect('index');
               
            }
        }
}
public function verify()
{

   ob_clean();
   $Verify = new \Think\Verify();
   $Verify -> entry();
}
    	}
